﻿package ascript.components.willard
{
	//#########################
	// Header Includes
	//#########################
	import ascript.Game;
	import Box2D.Common.Math.b2Vec2;
	import ascript.GameComponent;
	import ascript.GameLevel;
	import ascript.GameObject;
	import ascript.utilities.PointSimple;
	
	//#########################
	// GeneralSpin Class
	//#########################
	public class WillardPlayer extends GameComponent
	{
		//=========================
		// Constructors
		//=========================	
		public function WillardPlayer():void
		{
		}
		
		//=========================
		// Members
		//=========================
		override public function initComponent( tGame:Game, tObject:GameObject ):void
		{
			// Set Default Attributes
			defaultAttribute( tObject, "PillAmount", 5 );
			defaultAttribute( tObject, "PillIsActive", false );
			defaultAttribute( tObject, "PillDuration", 2000 );
			defaultAttribute( tObject, "PillTimer", 0 );
			defaultAttribute( tObject, "BottleIsActive", false );
			defaultAttribute( tObject, "BottleDuration", 5000 );
			defaultAttribute( tObject, "BottleTimer", 0 );
			defaultAttribute( tObject, "HeartRateLastTime", 0.0 );
			defaultAttribute( tObject, "HeartRateStageMarkers", new Array( 0, 0.3, 0.6, 1.0 ) );
			defaultAttribute( tObject, "HeartRateSoundDurations", new Array( 400, 400, 375, 400 ) );
			defaultAttribute( tObject, "HeartRate", 0.0 );
			defaultAttribute( tObject, "HeartRateStage", 0 );
			defaultAttribute( tObject, "HeartRateDuration", 2000 );
			defaultAttribute( tObject, "HeartRateTimer", 0 );
			defaultAttribute( tObject, "HeartRateSoundTimer", 0 );
			defaultAttribute( tObject, "VelocityMaxPill", new b2Vec2( 5, 5 ) );
			defaultAttribute( tObject, "VelocityMaxDefault", new b2Vec2( 4, 4 ) );
		}
		
		override public function applyAttribute( tGame:Game, tObject:GameObject, tAttribute:String ):void
		{
			switch( tAttribute )
			{
				case "AnimationDeathIsEnabled":
					if ( tObject.getAttribute( tAttribute ) )
					{
						tObject.getAttribute( "Physics" ).SetType( 0 );
						tObject.setAttribute( "AnimationCharacterIsEnabled", false );
						// GAME OVER LEVEL
					}
					else
					{
						// RESET PHYSICS
						// TURN OFF DEATH ANIM, PLAY REG ANIMS
					}
					break;
				case "Location":
					// Move level with player
					( tGame.gameLevel ).setAttribute( "Location", new b2Vec2( ( tObject.getAttribute( "LocationInitial" ).x - tObject.getAttribute( "Location" ).x ), ( tObject.getAttribute( "LocationInitial" ).y - tObject.getAttribute( "Location" ).y ) ) );
					break;
				case "PillAmount":
					( ( tGame.gameUI ).getAttribute( "PillCounter" ) ).text = String( tObject.getAttribute( "PillAmount" ) );
					break;
				case "PillIsActive":
					if ( tObject.getAttribute( tAttribute ) && tObject.getAttribute( "PillAmount" ) > 0 )
					{
						tObject.setAttribute( "PillTimer", tGame.time );
						tObject.setAttribute( "VelocityMax", tObject.getAttribute( "VelocityMaxPill" ) );
						tObject.setAttribute( "PillAmount", ( tObject.getAttribute( "PillAmount" ) - 1 ) );
						//( tGame.gameSound ).queueSound( ( tGame.gameAssets ).getAsset( "CueEffectPillUse" ) );
					}
					else if ( !tObject.getAttribute( tAttribute ) )
					{
						tObject.setAttribute( "VelocityMax", tObject.getAttribute( "VelocityMaxDefault" ) );
					}
					break;
				case "BottleIsActive":
					if ( tObject.getAttribute( tAttribute ) )
					{
						tObject.setAttribute( "BottleTimer", tGame.time );
						//( ( tGame.gameUI ).getAttribute( "MaskOuter" ) ).visible = false;
						//( ( tGame.gameUI ).getAttribute( "MaskInner" ) ).visible = false;
					}
					else
					{
						//( ( tGame.gameUI ).getAttribute( "MaskOuter" ) ).visible = true;
						//( ( tGame.gameUI ).getAttribute( "MaskInner" ) ).visible = true;
					}
					break;
				case "VelocityMaxDefault":
					tObject.setAttribute( "VelocityMax", tObject.getAttribute( tAttribute ) );
					break;
				case "HeartRate":
					var tempStage:uint = 0;
					var tempLength:uint = ( tObject.getAttribute( "HeartRateStageMarkers" ) ).length - 1;
					for ( var i:uint = 0; i < tempLength; i++ )
					{
						if ( tObject.getAttribute( "HeartRate" ) > tObject.getAttribute( "HeartRateStageMarkers" )[i] )
						{
							tempStage += 1;
						}
					}
									
					if ( tempStage != tObject.getAttribute( "HeartRateStage" ) )
					{
						//( ( tGame.gameUI ).getAttribute( "Heart" ) ).setGraphic( tempStage );
						//( ( tGame.gameUI ).getAttribute( "Heart" ) ).controlGraphic( ( ( 1 / tObject.getAttribute( "HeartRate" ) ) * ( ( tGame.gameUI ).getAttribute( "Heart" ) ).getFPS( true ) ) );
						tObject.setAttribute( "HeartRateStage", tempStage );
					}
					break;
				default:
					super.applyAttribute( tGame, tObject, tAttribute );
					break;
			}
		}
		
		override public function updateComponent( tGame:Game, tObject:GameObject, tLevel:GameLevel ):void
		{
			// Pill Timer
			if ( tObject.getAttribute( "PillIsActive" ) && ( tGame.time - tObject.getAttribute( "PillTimer" ) ) >= tObject.getAttribute( "PillDuration" ) )
			{
				tObject.setAttribute( "PillIsActive", false );
			}
			
			// Bottle timer
			if ( tObject.getAttribute( "BottleIsActive" ) && ( tGame.time - tObject.getAttribute( "BottleTimer" ) ) >= tObject.getAttribute( "BottleDuration" ) )
			{
				tObject.setAttribute( "BottleIsActive", false );
			}
			
			// Heart Rate Timer
			if ( ( tGame.time - tObject.getAttribute( "HeartRateTimer" ) ) >= tObject.getAttribute( "HeartRateDuration" ) )
			{
				( ( tGame.gameUI ).getAttribute( "MaskInner" ) ).rotation += 0.01;
				//tObject.setAttribute( "HeartRateTimer", tempTime );
				//tObject.setAttribute( "HeartRate", ( tObject.getAttribute( "HeartRate" ) + 0.05 ) );
			}
			
			// Heart Sound Timer
			//if ( tObject.getAttribute( "HeartRateStage" ) > 0 && ( tempTime - tObject.getAttribute( "HeartRateSoundTimer" ) ) >= ( ( 1 / tObject.getAttribute( "HeartRate" ) ) * tObject.getAttribute( "HeartRateSoundDurations" )[ tObject.getAttribute( "HeartRateStage" ) ] ) )
			//{
				//tObject.setAttribute( "HeartRateSoundTimer", tempTime );
				//( tGame.gameSound ).queueSound( ( tGame.gameAssets ).getAsset( "CueEffectHeart" )[ ( tObject.getAttribute( "HeartRateStage" ) - 1 ) ] );
			//}
		}
	}
}